package com.start.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.start.admin.entity.StartSysRoleDataRule;
import com.start.admin.mappers.SysRoleDataRuleMapper;
import com.start.admin.service.StartSysRoleDataRuleService;
import com.start.common.details.BaseUserDetails;
import com.start.common.exceptions.StartBusException;
import com.start.common.vo.SysMenuDataRulesVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class StartSysRoleDataRuleServiceImpl extends ServiceImpl<SysRoleDataRuleMapper, StartSysRoleDataRule> implements StartSysRoleDataRuleService {
    @Override
    public List<SysMenuDataRulesVo> listByUserId(String userId) {
        return getBaseMapper().listByUserId(userId);
    }

    @Override
    @Transactional
    public void saveDataRules(String roleId, String menuId, List<String> dataRuleIds) {
        if (StringUtils.isEmpty(roleId)) throw new StartBusException("角色id不能为空");
        if (StringUtils.isEmpty(menuId)) throw new StartBusException("菜单id不能为空");
        LambdaQueryWrapper<StartSysRoleDataRule> deleteWrapper = new LambdaQueryWrapper<>();
        deleteWrapper.eq(StartSysRoleDataRule::getRoleId, roleId);
        deleteWrapper.eq(StartSysRoleDataRule::getMenuId, menuId);
        remove(deleteWrapper);
        if (dataRuleIds == null || dataRuleIds.size() < 1) return;
        List<StartSysRoleDataRule> saveList = new ArrayList<>();
        for (String id : dataRuleIds) {
            StartSysRoleDataRule rule = new StartSysRoleDataRule();
            rule.setDataRuleId(id);
            rule.setMenuId(menuId);
            rule.setRoleId(roleId);
            saveList.add(rule);
        }
        saveBatch(saveList, 20);
    }

    @Override
    public List<StartSysRoleDataRule> getListByRoleIdAndMenuId(String roleId, String menuId) {
        LambdaQueryWrapper<StartSysRoleDataRule> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StartSysRoleDataRule::getRoleId, roleId);
        queryWrapper.eq(StartSysRoleDataRule::getMenuId, menuId);
        return list(queryWrapper);
    }

    /**
     * 根据用户以及组件查询数据规则
     *
     * @param userDetails
     * @param component
     * @return
     */
    @Override
    public List<SysMenuDataRulesVo> listByUserIdAndComponent(BaseUserDetails userDetails, String component) {
        return getBaseMapper().listByUserIdAndComponent(userDetails.getUserId(), component);
    }

    /**
     * 根据用户以及表单id查询数据规则
     *
     * @param userDetails
     * @param dynamicFormId
     * @return
     */
    @Override
    public List<SysMenuDataRulesVo> listByUserIdAndDynamicFormId(BaseUserDetails userDetails, String dynamicFormId) {
        return getBaseMapper().listByUserIdAndDynamicFormId(userDetails.getUserId(), dynamicFormId);
    }
}
